package com.zzb.config;


import com.zzb.interceptor.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


/**
 * @author ASUS
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /*@Autowired
    private UserDetailsService userDetailsService;*/

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    @Autowired
    private AccessDeniedHandler accessDeniedHandler;
    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Bean
    public PasswordEncoder password() {
        return new BCryptPasswordEncoder();
    }

 /*   @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(password());
    }*/


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
                // 关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 对请求进行认证的配置
                .authorizeRequests()
                // 对于登录接口,antMatchers()对里面的路径放行不做拦截,permiAll()表示不管登录或是没有登录都可以方法;
                .antMatchers("/code", "/upload", "/user/logout"
                        //swagger资源放行
                        , "/swagger-ui.html"
                        , "/webjars/**"
                        , "/swagger-resources/**"
                        , "/v2/**"
                        //图片资源放行
                        ,"/img/**"
                ).permitAll()
                // 对于登录接口,antMatchers()对里面的路径放行不做拦截,anonymous()允许匿名访问;
                .antMatchers("/login").anonymous()
                // 基于地址的权限配置，这个配置也可以在路径的方法名上配置@PreAuthorize("hasAuthority('all')")
                // 除上面外的所有请求全部需要权限认证
                .anyRequest().authenticated()
                .and().formLogin()
                //自定义登录页面
                .loginPage("http://localhost:63342/period7-web/web/html/login.html");

        // SpringSecurity配置，运行跨域请求
        http.cors();

        http
                //退出
                .logout()
                .logoutUrl("/logout").permitAll();

        http.exceptionHandling()
                //权限不足
                .accessDeniedHandler(accessDeniedHandler)
                //认证失败
                .authenticationEntryPoint(authenticationEntryPoint);

        //将自定义jwt验证拦截器放在UsernamePasswordAuthenticationFilter之前
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}
